<template>
  <div class="search">
    <!-- 1.输入搜索关键字的表单 large 设置高度-->
    <el-form :model="searchForm" ref="formRef" label-width="80px" size="large">
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="用户名" prop="name">
            <el-input v-model="searchForm.name" placeholder="请输入查询的用户名" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="真实姓名" prop="realname">
            <el-input v-model="searchForm.realname" placeholder="请输入查询的真实姓名" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="手机号码" prop="cellphone">
            <el-input v-model="searchForm.cellphone" placeholder="请输入查询的手机号码" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="状态" prop="enable">
            <el-select
              v-model="searchForm.enable"
              placeholder="请选择查询的状态"
              style="width: 100%"
            >
              <el-option label="启用" :value="1" />
              <el-option label="禁用" :value="0" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <!-- @change="ChangeRole" 这里直接绑定就行 不用改变的时候去做一些事 -->

          <el-form-item label="角色" prop="role">
            <el-select v-model="searchForm.role" placeholder="请选择查询的角色" style="width: 100%">
              <el-option v-for="item in Roles" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="创建时间" prop="createAt">
            <el-date-picker
              v-model="searchForm.createAt"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 2.重置和搜索的按钮 -->
    <div class="btns">
      <el-button icon="Refresh" @click="handleResetClick">重置</el-button>
      <el-button icon="Search" type="primary" @click="handleQueryClick">查询</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue'
import type { ElForm } from 'element-plus'
import { getRoleListRequest } from '@/service/Pub.ts'

// 定义自定义事件 子组件 发送给父组件，在父组件可以直接调用这个方法
const emit = defineEmits(['queryClick', 'resetClick'])

// interface RoleStr {
//   name: string
//   id: number
// }

type RoleStr = {
  name: string
  id: number
}

// 定义form的数据
const searchForm = reactive({
  name: '',
  realname: '',
  cellphone: '',
  enable: '',
  role: '',
  createAt: '',
})

const Roles = ref<RoleStr[]>([])
// const Roles = ref('')

// 获取权限
function getRoleSel() {
  getRoleListRequest(1).then((res) => {
    console.log('--------------getRoleListRequest----------------')
    console.log(res)
    Roles.value = res.data
  })
}

onMounted(() => {
  getRoleSel()
})

// function ChangeRole(v: any) {
//   searchForm.role = v
//   console.log('============', v)
// }

// 重置操作
const formRef = ref<InstanceType<typeof ElForm>>()
function handleResetClick() {
  // 1.form中的数据全部重置
  formRef.value?.resetFields()

  // 2.将事件出去, content内部重新发送网络请求
  emit('resetClick')
}

// 流程 -》 发送给父组件，再发送给content 兄弟组件
function handleQueryClick() {
  // 把点击时事件发送到父页面    父页面 <USearch @query-click="userHandleQueryClick" /> query-click 一定是中横线
  // 最后再把事件发送给兄弟组件
  emit('queryClick', searchForm)
}
</script>

<style lang="less" scoped>
.search {
  background-color: #fff;
  padding: 20px;

  .el-form-item {
    padding: 5px 2px;
    margin-bottom: 0;
  }

  .btns {
    text-align: right;
    padding: 0 50px 10px 0;

    .el-button {
      height: 36px;
    }
  }
}
</style>
